package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.InFo.HeroGuideInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/5.
 */
public class HeroGuideAdapter extends BaseAdapter {
    private List<HeroGuideInfo.DataBean> heroGuideInfos;
    private Context context;

    public HeroGuideAdapter(List<HeroGuideInfo.DataBean> heroGuideInfos, Context context) {
        this.heroGuideInfos = heroGuideInfos;
        this.context = context;
    }

    @Override
    public int getCount() {
        return heroGuideInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return heroGuideInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHold viewHold = null;
        if (viewHold == null) {
            viewHold = new ViewHold();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_heroguide, null);
            viewHold.iv = (ImageView) convertView.findViewById(R.id.hero_guide_iv);
            viewHold.icon = (ImageView) convertView.findViewById(R.id.hero_guide_icon);
            viewHold.title = (TextView) convertView.findViewById(R.id.hero_guide_title);
            viewHold.time = (TextView) convertView.findViewById(R.id.hero_guide_time);
            viewHold.comment = (TextView) convertView.findViewById(R.id.hero_guide_comment);
            convertView.setTag(viewHold);
        } else {
            viewHold = (ViewHold) convertView.getTag();

        }
        HeroGuideInfo.DataBean bean = heroGuideInfos.get(position);
        if (bean.getPic_url() == null || bean.getPic_url().isEmpty()) {
            viewHold.icon.setVisibility(View.GONE);
            viewHold.iv.setVisibility(View.GONE);
        } else {
            Picasso.with(context).load(bean.getPic_url()).into(viewHold.iv);
        }
        viewHold.title.setText(bean.getTitle());
        viewHold.comment.setText(bean.getComment() + " 评");
        int date_published = bean.getPublished();
        long date = date_published * 1000L;
        Date d = new Date(date);
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
        String dateStr = format.format(d);
        viewHold.time.setText(dateStr);
        return convertView;


    }

    private class ViewHold {
        private ImageView iv, icon;
        private TextView title, time, comment;
    }
}
